package com.umeox.watch.calendar;


import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.umeox.moto.common.log.Logger;
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.moto.watch.themes.widget.TextView;

import java.util.Locale;


public class MainActivity extends ThemeAppCompatActivity implements CalendarView.OnCalendarSelectListener {

    private static final int REQUEST_CODE_MONTH = 100;
    private static final int REQUEST_CODE_YEAR = 101;
    private TextView mYearTv;
    private TextView mMonthTv;
    private TextView mMonthYearTv;

    private CalendarView mCalendarView;
    private String[] weeks;
    private String[] months;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        weeks = getResources().getStringArray(R.array.weeks_string_array);
        months = getResources().getStringArray(R.array.months_string_array);
        initView();
    }

    private void initView() {
        mCalendarView = findViewById(R.id.calendarView);
        mYearTv = findViewById(R.id.year_tv);
        mMonthTv = findViewById(R.id.month_tv);
        mMonthYearTv = findViewById(R.id.month_year_tv);

        mCalendarView.setOnCalendarSelectListener(this);

        mYearTv.setText(String.format("%s", String.valueOf(mCalendarView.getCurYear()).substring(2)));
        mMonthTv.setText(months[mCalendarView.getCurMonth() - 1]);

        mMonthYearTv.setText(String.format(Locale.ENGLISH, "%s.%d", String.valueOf(mCalendarView.getCurYear()).substring(2), mCalendarView.getCurMonth()));

        findViewById(R.id.iv_pre).setOnClickListener(v -> {
            Logger.e("onClick========iv_pre");
            mCalendarView.scrollToPre(true);
        });

        findViewById(R.id.iv_next).setOnClickListener(v -> {
            Logger.e("onClick========iv_next");
            mCalendarView.scrollToNext(true);
        });

        mMonthTv.setOnClickListener(v -> {

            Intent chooseMonthIntent = new Intent(MainActivity.this, ChooseActivity.class);
            chooseMonthIntent.putExtra(ChooseActivity.CHOOSE_EXTRA, mCalendarView.getSelectedCalendar().getMonth());
            startActivityForResult(chooseMonthIntent, REQUEST_CODE_MONTH);
        });

        mYearTv.setOnClickListener(v -> {
            Intent chooseYearIntent = new Intent(MainActivity.this, ChooseActivity.class);
            chooseYearIntent.putExtra(ChooseActivity.CHOOSE_EXTRA, mCalendarView.getSelectedCalendar().getYear());
            startActivityForResult(chooseYearIntent, REQUEST_CODE_YEAR);
        });

    }

    @Override
    public void onCalendarOutOfRange(Calendar calendar) {

    }

    @Override
    public void onCalendarSelect(Calendar calendar, boolean isClick) {

        mYearTv.setText(String.format("%s", String.valueOf(calendar.getYear()).substring(2)));
        mMonthTv.setText(months[calendar.getMonth() - 1]);

        mMonthYearTv.setText(String.format(Locale.ENGLISH, "%s.%d", String.valueOf(calendar.getYear()).substring(2), calendar.getMonth()));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && data != null) {
            if (requestCode == REQUEST_CODE_MONTH) {
                int month = data.getExtras().getInt("value");
                mCalendarView.scrollToCalendar(mCalendarView.getSelectedCalendar().getYear(), month, mCalendarView.getSelectedCalendar().getDay());
            } else if (requestCode == REQUEST_CODE_YEAR) {
                int year = data.getExtras().getInt("value");
                mCalendarView.scrollToCalendar(year, mCalendarView.getSelectedCalendar().getMonth(), mCalendarView.getSelectedCalendar().getDay());
            }

        }
    }
}